package com.yuxi.Util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class ExportWord {
	private Map<String,String> dataMap = new HashMap<String,String>();
    /**
     * 设置标识值
     * @param tagList 标识
     * @param dataList 数据
     * @param dataMap
     */
    public void setData(List<String> tagList,List<String> dataList){
        Iterator<String> it1 = tagList.iterator();
        Iterator<String> it2 = dataList.iterator();
        while(it1.hasNext()){
            this.dataMap.put(it1.next(), it2.next());
        }
    }
    
    /**
     * 载入一个xml文档
     * @param filename 文件路径
     * @return 成功返回Document对象，失败返回null
     */
	 public Document LoadXml(String filename){
	        
	        Document doc = null;
	        try {
	            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
	            DocumentBuilder builder = factory.newDocumentBuilder();
	            doc = (Document) builder.parse(new File(filename));
	        } catch (Exception e) {
	            System.out.println("载入xml文件时出错");
	            e.printStackTrace();
	        }
	        return doc;
	    }
	 
	 
	 /** 
	  * doc2XmlFile 
	  * 将Document对象保存为一个xml文件 
	  * @return true:保存成功  flase:失败 
	  * @param filename 保存的文件名 
	  * @param document 需要保存的document对象 
	  */ 
	 public boolean doc2XmlFile(Document document,OutputStream os) 
	 { 
	    boolean flag = true; 
	    try{
	        TransformerFactory transFactory = TransformerFactory.newInstance(); 
	           Transformer transformer = transFactory.newTransformer();
	        DOMSource source=new DOMSource();
	        source.setNode(document);
	        StreamResult result=new StreamResult();
	        result.setOutputStream(os);
	        transformer.transform(source,result);        
	    }catch(Exception ex){ 
	        flag = false; 
	        ex.printStackTrace(); 
	    } 
	    return flag;       
	 }
	 
	 /**
	  * 替换标识内容：多个文本标记
	  * @param element 要替换内容的节点
	  * @return 返回替换后的节点
	  * @throws Exception
	  */
	 public Element replaceTagContext(Element  element){
	     Element xElement = element;
	     NodeList tElements = xElement.getElementsByTagName("w:t");//w:t标签组 
	     Set<String> dataSet = this.dataMap.keySet();
	     Iterator<String> it = dataSet.iterator();
	     while(it.hasNext()){
	         String tag = it.next();
	         String data = dataMap.get(tag);
	         for(int i=0; i<tElements.getLength(); i++){
	             Element tElement = (Element)tElements.item(i);
	             if(tElement.getTextContent().equals(tag)){
	                  tElement.setTextContent(data);
	              }
	         }
	     }
	     return xElement;
	 }
}
